<template>
  <div class="login-container">
    <!-- 背景视频  -->
    <video id="v1" autoplay loop muted>
      <source type="video/mp4" src="@/assets/common/login.mp4" />
    </video>
    <div class="one">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="rules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <!-- 放置标题图片 @是设置的别名-->
        <div class="title-container">
          <h3 class="title">
            <img src="@/assets/common/login.png" />
          </h3>
        </div>
        <!-- 账号 -->
        <el-form-item prop="account">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="account"
            v-model="loginForm.account"
            placeholder="账号"
            name="account"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <span class="svg-container">
            <i slot="prefix" class="el-input__icon el-icon-s-claim" />
          </span>
          <el-input
            v-model="loginForm.code"
            class="imga"
            placeholder="验证码"
          />
          <div class="imgSty">
            <img @click="replace" :src="imgSite" alt="" />
          </div>
        </el-form-item>

        <!-- 登录按钮 -->
        <el-button
          class="loginBtn"
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
        >
          登录
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { captchaApi } from '@/api/user'
import { mapActions } from 'vuex'
// import { Message } from 'element-ui'
export default {
  name: 'Login',

  data() {
    // const validateUsername = (rule, value, callback) => {
    //   if (!validUsername(value)) {
    //     callback(new Error('Please enter the correct user name'))
    //   } else {
    //     callback()
    //   }
    // }
    // const validatePassword = (rule, value, callback) => {
    //   if (value.length < 6) {
    //     callback(new Error('The password can not be less than 6 digits'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      imgSite: '',
      loginForm: {
        account: 'shenlingadmin',
        password: '123456',
        code: '',
        key: 'key'
      },
      rules: {
        // 登录账号
        account: [
          {
            required: true,
            message: '请输入账号',
            trigger: 'blur'
          },
          {
            min: 11,
            max: 16,
            message: '请输入正确的账号',
            trigger: 'blur'
          }
        ],
        // 登录密码
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 10,
            message: '长度在 6 到 10 个字符',
            trigger: 'blur'
          }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  created() {
    this.verification()
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    ...mapActions('user', ['getlogin']),
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 验证码
    async verification() {
      const res = await captchaApi()
      this.imgSite = res.request.responseURL
      // console.log(res.request.responseURL)
    },
    // 点击登录
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        // 校验不成功直接跳出
        if (!valid) return
        this.loading = true
        this.getlogin(this.loginForm)
          .then(() => {
            this.$message.success('登录成功')
            this.$router.push('/')
          })
          .catch((err) => {
            if (err) {
              // 打印出错误
              console.log(err)
              // 验证码超时或者错误刷新验证码
              this.replace()
            }
          })
          .finally(() => {
            this.loading = false
          })
      })
    },
    // 点击验证码刷新
    replace() {
      const num = Math.ceil(Math.random() * 10) // 生成一个随机数（防止缓存）
      // 这个数返回给key,
      // 不管是刷新后请求验证码的key还是刚进页面请求验证码的key都
      // 要和登录请求的key相等才能判断登录
      this.loginForm.key = num
      // console.log(this.loginForm.key)
      this.imgSite = 'http://localhost:9991/manager/captcha?key=' + num
      // console.log(this.imgSite)
    }
  }
}
</script>

<style lang="scss">
* {
  margin: 0px;
  padding: 0px;
}
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$cursor: #fff;
$light_gray: #000; // 修改输入框字体颜色

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
// 验证码图标偏右问题
.el-input__icon {
  text-align: left;
}

// 背景视频
video {
  position: fixed;
  right: 0px;
  bottom: 0px;
  min-width: 100%;
  min-height: 100%;
  height: auto;
  width: auto;
}
source {
  min-width: 100%;
  min-height: 100%;
  height: auto;
  width: auto;
}
//-----

/* reset element-ui css */
.login-container {
  position: relative;
  // 中间盒子
  .one {
    border-radius: 20px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    height: 466px;
    width: 450px;
    background-color: #fff;
  }
  .el-input {
    display: inline-block;
    height: 46px;
    width: 85%;

    input {
      caret-color: #606266;
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 46px;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #dcdfe6;
    background: rgba(255, 255, 255, 0.9); // 输入登录表单的背景色
    border-radius: 5px;
    color: #454545;
    width: 368px;
    height: 48px;
    margin-top: 26px;
  }
  .loginBtn {
    margin-top: 22px;
    background: #e15536;
    padding: 12px 20px;
    height: 56px;
    width: 330px;
    font-size: 18px;
    color: #fff;
    font-weight: 700;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 36px 40px;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 0 24px;
      text-align: center;
      font-weight: bold;
      img {
        width: 196px;
        height: 55px;
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
// 验证码框大小
.imga {
  width: 220px;
}
// 验证码图片
.imgSty {
  height: 44px;
  float: right;
  margin-top: 4px;
  margin-right: 10px;
  img {
    width: 90px;
    height: 40px;
  }
}
</style>
